package com.macasa.service.dal.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.macasa.panel.common.log.Logger;
import com.macasa.panel.common.log.annotations.ILoggable;
import com.macasa.panel.common.utils.CastHelpers;
import com.macasa.panel.dao.hibernate.api.IDao;
import com.macasa.panel.dao.hibernate.api.IUserDao;
import com.macasa.panel.entity.api.IUser;
import com.macasa.panel.exception.DALException;
import com.macasa.panel.exception.DAOException;
import com.macasa.service.dal.api.IUserDal;

@Service("userDal")
@Scope("singleton")
@Transactional
public class UserDal extends AbstractCRUDDal<IUser, Integer> implements IUserDal {

	/** long serialVersionUID */
	private static final long	serialVersionUID	= 5133633554369854431L;

	@Override
	@Autowired
	@Qualifier("userDao")
	public void setDao(final IDao<IUser, Integer> ao) {
		super.setDao(ao);
	}

	@Override
	@Transactional(readOnly = true)
	@ILoggable
	public IUser getUserByLogin(final String login) throws DALException {
		try {
			return CastHelpers.cast(this.getDao(), IUserDao.class).getUserByLogin(login);
		} catch (final DAOException e) {
			Logger.error("getUserByLogin failed", e);
			throw new DALException(e, DALException.ERROR_CODE.DATABASE_ERROR);
		}
	}
}
